package com.cl.controller;

import com.alibaba.fastjson.JSON;
import com.cl.pojo.User;
import com.cl.service.impl.UserInfoServiceImpl;
import com.cl.service.impl.UserServiceImpl;
import com.cl.utils.CodeUtil;
import com.cl.utils.MegUtil;
import com.cl.utils.UuidUtil;
import com.sun.javafx.binding.StringFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private UserInfoServiceImpl userInfoService;
    @RequestMapping("/tologin")
    public String toLogin(){
        return "login";
    }
    @RequestMapping("/toregister")
    public String toregister(){
        return "register";
    }
    @RequestMapping("/toinfo")
    public String toinfo(){
        return "userinfo";
    }


    //注册
    @ResponseBody
    @RequestMapping("/register")
    public String register(User user){
        int uid = UuidUtil.getUid();
        user.setUid(uid);
        int i = userService.insertUser(user);
        userInfoService.insertUid(uid);
        return JSON.toJSONString(i);
    }

    //验证码
    @ResponseBody
    @RequestMapping("/getcode")
    public void getCode(HttpServletRequest req, HttpServletResponse resp){
        try {
            CodeUtil.getCode(req,resp);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //登录
    @ResponseBody
    @RequestMapping("/login")
    public String login(@RequestParam("email") String email,@RequestParam("password") String password,@RequestParam("code") String code,HttpServletRequest req){
        HttpSession session = req.getSession();
        char[] verification = (char[]) session.getAttribute("verification");
        String sessioncode = new String(verification);
        if (sessioncode.toUpperCase().equals(code.toUpperCase())){
            User user = userService.queryByEmail(email);
            if (user!=null){
                if (user.getPassword().equals(password)){
                    session.setAttribute("login",user);
                    return JSON.toJSONString(user);
                }else return JSON.toJSONString(MegUtil.fail("密码错误"));
            }else return JSON.toJSONString(MegUtil.fail("账号错误"));
        }else return JSON.toJSONString(MegUtil.fail("验证码错误"));
    }

    //修改密码
    @ResponseBody
    @RequestMapping("/updatepwd")
    public String updatePwd(@RequestParam("email")String email,@RequestParam("password")String password){
        User user = userService.queryByEmail(email);
        if (user!=null){
            int lid = user.getLid();
            int i = userService.updatePwd(lid, password);
            return JSON.toJSONString(MegUtil.success("修改成功"));
        }else return JSON.toJSONString(MegUtil.fail("修改失败"));
    }

    @ResponseBody
    @RequestMapping("/logout")
    public String logOut(HttpServletRequest req){
        HttpSession session = req.getSession();
        session.removeAttribute("login");
        return JSON.toJSONString(MegUtil.success("注销成功"));
    }
}


